package com.sjy.ylxt.controller;


import com.alibaba.fastjson2.annotation.JSONField;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.domain.FileEntity;
import com.sjy.ylxt.entity.dto.UploadParam;
import com.sjy.ylxt.service.FileService;
import com.sjy.ylxt.service.YlxtAdminService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;
    @PostMapping("/upload")
//    @RequestBody
    public ResponseEntity<Long> uploadFile(UploadParam file) throws Exception{
        try {
            FileEntity fileEntity = new FileEntity();
            fileEntity.setFileName(file.getFile().getOriginalFilename());
            fileEntity.setFileType(file.getFile().getContentType());
            fileEntity.setFileSize(file.getFile().getSize());
            fileEntity.setData(file.getFile().getBytes());
            fileEntity.setUploadTime(new Date());
            fileService.insert(fileEntity);
            System.out.println("fileEntity.getId(): "+fileEntity.getId());
            return ResponseEntity.status(HttpStatus.OK)
                    .body(fileEntity.getId());//+ file.getOriginalFilename());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(0L);
        }
    }

    @GetMapping("/detail")
    public ResultBody<?> getFile(@RequestParam(value = "id") Long id) {
//    public ResponseEntity<Map<String, Object>> getFile(@RequestParam(value = "id") Long id) {
        FileEntity fileEntity = fileService.findById(id);

//        if (null == fileEntity) {
//            return ResponseEntity.notFound().build();
//        }

        // 将二进制数据转换为Base64
        String base64Data = Base64.getEncoder().encodeToString(fileEntity.getData());
        fileEntity.setBase64Data(base64Data);
//        Map<String, Object> response = new HashMap<>();
//        response.put("id", fileEntity.getId());
//        response.put("fileName", fileEntity.getFileName());
//        response.put("fileType", fileEntity.getFileType());
//        response.put("fileSize", fileEntity.getFileSize());
//        response.put("uploadTime", fileEntity.getUploadTime().toString());
//        response.put("data", base64Data);
//
//        return ResponseEntity.ok(response);
        return ResultBody.success(fileEntity);
    }
//
//    @GetMapping("/files")
//    public ResponseEntity<List<FileEntity>> getAllFiles() {
//        return ResponseEntity.ok(fileRepository.findAll());
//    }
}


//public interface FileRepository extends JpaRepository<FileEntity, Long> {
//}